package com.syh.jn.scm.infrastructure.repository.impl.supplier.system;

import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AggregateRepoMybatisImplTmpl;
import com.syh.jn.scm.domain.supplier.repository.system.SupplierAccountOfPhoneReadRepository;
import com.syh.jn.scm.domain.supplier.repository.system.SupplierAccountReadRepository;
import com.syh.jn.scm.dto.supplier.system.supplierAccount.SupplierAccountVo;
import com.syh.jn.scm.dto.supplier.system.supplierAccountOfPhone.SupplierAccountOfPhoneVo;
import com.syh.jn.scm.infrastructure.repository.model.system.AccountModel;
import com.syh.jn.scm.infrastructure.repository.model.system.AccountOfPhoneModel;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by soci.song on 2018/12/13.
 */
@Log
@Repository("supplierAccountOfPhoneReadRepositoryImpl")
public class SupplierAccountOfPhoneReadRepositoryImpl implements SupplierAccountOfPhoneReadRepository {

  private AggregateRepoMybatisImplTmpl<SupplierAccountOfPhoneVo, AccountOfPhoneModel, Long> mybatisImplTmpl = new AggregateRepoMybatisImplTmpl.Builder()
          .listTypeClass(SupplierAccountOfPhoneVo.class).modelClass(AccountOfPhoneModel.class).build();
  @Autowired
  private SupplierAccountReadRepository supplierAccountReadRepository;

  @Override
  public SupplierAccountOfPhoneVo getOne(Wrapper wrapper) {
    SupplierAccountOfPhoneVo vo = mybatisImplTmpl.getOne(wrapper);
    if (vo != null && vo.getId() != null) {
      Wrapper accountWe = Wrapper.build().append(Restrictions.eq("loginPhoneId", vo.getId()));
      List<SupplierAccountVo> accountVos = supplierAccountReadRepository.getListView(accountWe);
      vo.setAccounts(accountVos);
    }
    return vo;
  }

  @Override
  public List<SupplierAccountOfPhoneVo> getList(Wrapper wrapper) {
    return this.findListPage(wrapper, null, null);
  }

  @Override
  public List<SupplierAccountOfPhoneVo> findListPage(Wrapper wrapper, Integer startRow, Integer count) {
    return mybatisImplTmpl.findListPage(wrapper, startRow, count);
  }

  @Override
  public SupplierAccountOfPhoneVo getById(Long aLong) {
    return mybatisImplTmpl.getById(aLong);
  }

  @Override
  public boolean contains(Long aLong) {
    return mybatisImplTmpl.contains(aLong);
  }

  @Override
  public int getCount(Wrapper wrapper) {
    return mybatisImplTmpl.getCount(wrapper);
  }
}
